package com.Servlet;

import com.bean.User;
import com.dao.impl.UserDaoImpl;
import com.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "UserServlet" ,urlPatterns = "/regist")
public class UserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        User user = new User();
        UserServiceImpl userService = new UserServiceImpl();

        user.setUsername(request.getParameter("username"));
        user.setPassword(request.getParameter("password"));
        user.setEmail(request.getParameter("email"));
        //判断输入的名字是否和数据库的有重复
        if (userService.insertUser(user) >0){
//            userService.queryUserByUsername(user.getUsername());
            request.setAttribute("user",user);
            //跳转登录成功的界面
            request.getRequestDispatcher("/regist_success.jsp").forward(request,response);

        }else{
            request.getRequestDispatcher("/regist.jsp").forward(request,response);

        }

      }
}
